/**
 * Created by jackliu on 15/12/16.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var TgReviewHisModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //`his_id` bigint(20) unsigned NOT NULL COMMENT '历史记录id',
  //`design_id` bigint(20) unsigned NOT NULL COMMENT '设计模版id',
  //`state` int(2) NOT NULL DEFAULT '0' COMMENT '审核状态',
  //`content` text COMMENT '审核内容',
  //`reviewer` bigint(20) unsigned NOT NULL COMMENT '审核者id',
  //`create_time` datetime NOT NULL,
  //`update_time` datetime NOT NULL,
  //`update_by` varchar(50) NOT NULL DEFAULT '',
  //`del_flg` int(1) NOT NULL DEFAULT '0'
};
TgReviewHisModel.prototype = new ItbBaseModel();

// 取得表名
TgReviewHisModel.prototype.getTableName = function(){
    return 'tg_review_his';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
TgReviewHisModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'his_id='+this.his_id;
}

//------------------------------------------------------------------
/* 获取指定设计最新审核状态
 * 参数:design_id
 * 参数:callback:回调
 * */
TgReviewHisModel.prototype.getLatestHis = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = ' SELECT tt.* FROM ('+
            ' SELECT r.design_id, r.state, r.content '+
            ' FROM TG_DESIGN_DOC t, TG_REVIEW_HIS r '+
            ' WHERE t.del_flg=0 AND t.design_id= '+THIS.design_id+
            ' AND r.del_flg=0 AND r.design_id=t.design_id '+
            ' ORDER BY r.update_time DESC )tt LIMIT 1';

        //var sqlValue = [THIS.type_id];
        //sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            //console.log();

            callback(error, result);
        });
    });
};

/**
 * 获取最后一条审核记录
 *
 * @author 王俊
 * @date 2018-04-16
 *
 * @param {Object} options 请求参数
 * @param {Number} options.design_kind 10 静态设计, 20 视频设计
 * @param {Number} options.design_id 模板唯一标识
 * @param {Number} options.state_type 1上架流程状态, 2下架流程状态
 * @param {Number} options.state 上架审核状态: 10制作中;20提交等待审核;31一级审核通过,32二级审核通过;41一级审核失败,42二级审核失败\\n下架审核状态: 20申请下架待审核;31一级审核通过,32二级审核通过;41一级审核失败,42二级审核失败
 * @param {Function} callback 回调函数
 *                      err: 查询失败
 *                      his: 记录行对象
 */
TgReviewHisModel.prototype.getLatestReviewHistory = function (options, callback) {
    pool.pool.getConnection(function(getConErr, connection) {
        if(getConErr) {
            console.log('TgReviewHisModel.prototype.getLatestReviewHistory getConErr: ' + getConErr);
            return callback(getConErr, null);
        }
        var sql =
            ' SELECT * ' +
            ' FROM itbour.tg_review_his trh ' +
            ' WHERE trh.del_flg = 0 ';
        if (options.design_kind === 10 || options.design_kind === 20) {
            sql += '   AND trh.kind = ' + options.design_kind + ' ';
        }
        if(options.design_id > 0) {
            sql += '   AND trh.design_id = ' + options.design_id + ' ';
        }
        if(options.state_type === 1 || options.state_type === 2) {
            sql += '   AND trh.state_type = ' + options.state_type + ' ';
        }
        if(options.state) {
            sql += '   AND trh.state = ' + options.state + ' ';
        }
        sql += ' ORDER BY trh.update_time DESC LIMIT 1 ';

        console.log(sql);
        connection.query(sql, function(queryErr, results) {
            connection.release();
            if(queryErr || !results || results.length <=0) {
                console.log('TgReviewHisModel.prototype.getLatestReviewHistory queryErr: ' + queryErr);
                return callback(queryErr, null)
            }
            return callback(queryErr, results[0]);
        });
    });
};




//--------------------------------end-------------------------------


// 导出
module.exports = TgReviewHisModel;